/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.web.controller;


import cc.altius.model.Vendor;
import cc.altius.model.HealthSpringCenter;
import cc.altius.model.VendorDetail;
import cc.altius.model.VendorSubtype;
import cc.altius.service.CenterService;
import cc.altius.service.VendorService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

/**
 *
 * @author jitendra
 */
@Controller
public class VendorController {
    @Autowired
    VendorService vendorService;
    @Autowired
    CenterService centerService;

    @ModelAttribute("HealthSpringCenter")
    public List<HealthSpringCenter> getHealthSpringCenter() {
        return this.centerService.getCenterList();
    }

    @ModelAttribute("VendorDetail")
    public List<VendorDetail> getVendorDetailList() {
        return this.vendorService.getVendorDetailList();
    }

    @ModelAttribute("VendorSubtype")
    public List<VendorSubtype> getVendorSubList() {
        return this.vendorService.getVendorSubtypeList();
    }

    @RequestMapping(value = "vendor/addVendor.htm", method = RequestMethod.GET)
    public String showAddvendor(@ModelAttribute("vendor") Vendor vendor) {
        System.out.println("inside vendor add get");
        return "vendor/addVendor";
    }

    @RequestMapping(value = "vendor/addVendor.htm", method = RequestMethod.POST)
    public String AddVendor(@ModelAttribute("vendor") Vendor vendor, SessionStatus sessionStatus) {
        this.vendorService.addVendor(vendor);
        sessionStatus.setComplete();
        return "redirect:listVendor.htm";
    }

    @RequestMapping(value = "vendor/listVendor.htm")
    public String showListVendor(@ModelAttribute("vendor") Vendor vendor, HttpServletRequest request, ModelMap model) {
        int centerId = ServletRequestUtils.getIntParameter(request, "healthSpringCenter.centerId", 0);
        int vendorId = ServletRequestUtils.getIntParameter(request, "vendorDetail.vendorDetailId", 0);
        List<Vendor> vendorList = this.vendorService.getVendorList(centerId, vendorId);
        model.addAttribute("vendorList", vendorList);
        return "vendor/listVendor";
    }

    @RequestMapping(value = "vendor/addVendorDetail.htm", method = RequestMethod.GET)
    public String showAddVendorDetail(@ModelAttribute("vendorDetail") VendorDetail vendorDetail) {
        return "vendor/addVendorDetail";
    }

    @RequestMapping(value = "vendor/addVendorDetail.htm", method = RequestMethod.POST)
    public String AddVendorDetail(@ModelAttribute("vendorDetail") VendorDetail vendorDetail) {
        this.vendorService.addVendorDetail(vendorDetail);
        return "redirect:addVendor.htm?vendorDetail.vendorDetailId="+vendorDetail.getVendorDetailId();
    }

    @RequestMapping(value = "vendor/editVendor.htm", method = RequestMethod.GET)
    public String showEditVendor(ModelMap modelmap, HttpServletRequest request, SessionStatus sessionStatus) {
        int vendorID = ServletRequestUtils.getIntParameter(request, "vendorId", 0);
        Vendor vendor = this.vendorService.getVendorById(vendorID);
        List<VendorSubtype> VendorSubtypeList = this.vendorService.getVendorSubListFromType(vendor.getVendorSubtype().getVendorType());
        modelmap.addAttribute("vendor", vendor);
        modelmap.addAttribute("VendorSubtypeList", VendorSubtypeList);
        
        sessionStatus.setComplete();
        return "vendor/editVendor";
    }

    @RequestMapping(value = "vendor/editVendor.htm", method = RequestMethod.POST)
    public String Updatevendor(@ModelAttribute("vendor") Vendor vendor, SessionStatus sessionStatus) {
        this.vendorService.updateVendor(vendor);
        sessionStatus.setComplete();
        return "redirect:listVendor.htm";
    }
    @RequestMapping(value = "vendor/editVendorDetail.htm", method = RequestMethod.GET)
    public String showEditVendorDetail(ModelMap modelmap, HttpServletRequest request, SessionStatus sessionStatus) {
        int vendorDetailId = ServletRequestUtils.getIntParameter(request, "vendorDetailId", 0);
        int vendorId = ServletRequestUtils.getIntParameter(request, "vendorId", 0);
        VendorDetail vendorDetail = this.vendorService.getVendorDetailById(vendorDetailId);
        modelmap.addAttribute("vendorDetail", vendorDetail);
        modelmap.addAttribute("vendorId",vendorId);
        sessionStatus.setComplete();
        return "vendor/editVendorDetail";
    }

    @RequestMapping(value = "vendor/editVendorDetail.htm", method = RequestMethod.POST)
    public String UpdateVendorDetail(@ModelAttribute("vendorDetail") VendorDetail vendorDetail,HttpServletRequest request, SessionStatus sessionStatus) {
        int vendorId=ServletRequestUtils.getIntParameter(request, "vendorId",0);
        this.vendorService.updateVendorDetail(vendorDetail);
        sessionStatus.setComplete();
        return "redirect:editVendor.htm?vendorId="+vendorId;
    }
    
    @RequestMapping(value = "ajaxCallForVendorSubList.htm", method = RequestMethod.GET)
    public @ResponseBody
    String doAjaxVendorSubPopulate(@RequestParam(value = "vendorType") String vendorType) {
        System.out.println("inside ajax call.........");
        String json;
        List<VendorSubtype> dispositionList = this.vendorService.getVendorSubListFromType(vendorType);
        Gson gson = new Gson();
        List list = new ArrayList();
        for (VendorSubtype d : dispositionList) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", d.getVendorSubId());
            map.put("label", d.getVendorSubtypeName());
            map.put("value", d.getVendorSubId());
            list.add(map);
        }
        Type typeList = new TypeToken<List>() {
        }.getType();
        json = gson.toJson(list, typeList);
        System.out.println("json :" + json);
        return json;
    }
}
